package com.wust.baby;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.wust.database.DatabaseFactory;
import com.wust.util.ConstDefine;

public class BabyDao {
	private Context context;
	private List<Baby>babylist;
	public BabyDao(Context context)
	{
		this.context=context;
	}
	public void insertBaby(Baby baby)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getWritableDatabase();
		ContentValues value=new ContentValues();
		value.put("baby_name", baby.getBaby_name());
		value.put("baby_icon", baby.getBaby_icon());
		value.put("baby_date", baby.getBaby_date());
		value.put("user_name", baby.getUser_name());
		value.put("baby_sex", baby.getBaby_sex());
		db.insert(ConstDefine.DatabaseFactoryUtil.BABY_TABLE, null, value);
		db.close();
	}
	public List<Baby>getBabyList(String user_name)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.BABY_TABLE, null,"user_name=?", new String[]{user_name}, null, null, "babydate asc");
		babylist=new ArrayList<Baby>();
		while(cursor.moveToNext())
		{
			Baby baby=new Baby();
			baby.setBaby_id(cursor.getInt(cursor.getColumnIndex("baby_id")));
			baby.setBaby_name(cursor.getString(cursor.getColumnIndex("baby_name")));
			baby.setBaby_icon(cursor.getString(cursor.getColumnIndex("baby_icon")));
			baby.setBaby_date(cursor.getString(cursor.getColumnIndex("baby_date")));
			baby.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
			baby.setBaby_sex(cursor.getString(cursor.getColumnIndex("baby_sex")));
			babylist.add(baby);
		}
		cursor.close();
		db.close();
		return babylist;
	}
	public Baby getBabyByName(String babyName,String user_name)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.BABY_TABLE, null, "baby_name=? and user_name=?", new String[]{babyName,user_name}, null, null, null);
		Baby baby=new Baby();
		while(cursor.moveToNext())
		{
			baby.setBaby_id(cursor.getInt(cursor.getColumnIndex("baby_id")));
			baby.setBaby_name(cursor.getString(cursor.getColumnIndex("baby_name")));
			baby.setBaby_icon(cursor.getString(cursor.getColumnIndex("baby_icon")));
			baby.setBaby_date(cursor.getString(cursor.getColumnIndex("baby_date")));			
			baby.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
			baby.setBaby_sex(cursor.getString(cursor.getColumnIndex("baby_sex")));
			
		}
		cursor.close();
		db.close();
		return baby;
	}
	public Baby getBabyByUserName(String user_name)
	{
		DatabaseFactory database=new DatabaseFactory(context,ConstDefine.DatabaseFactoryUtil.DATABASE_NAME);
		SQLiteDatabase db=database.getReadableDatabase();
		Cursor cursor=db.query(ConstDefine.DatabaseFactoryUtil.BABY_TABLE, null, "user_name=?", new String[]{user_name}, null, null, null);
		Baby baby=new Baby();
		while(cursor.moveToNext())
		{
			baby.setBaby_id(cursor.getInt(cursor.getColumnIndex("baby_id")));
			baby.setBaby_name(cursor.getString(cursor.getColumnIndex("baby_name")));
			baby.setBaby_icon(cursor.getString(cursor.getColumnIndex("baby_icon")));
			baby.setBaby_date(cursor.getString(cursor.getColumnIndex("baby_date")));			
			baby.setUser_name(cursor.getString(cursor.getColumnIndex("user_name")));
			baby.setBaby_sex(cursor.getString(cursor.getColumnIndex("baby_sex")));
			
		}
		cursor.close();
		db.close();
		return baby;
	}
}
